<template>
  <view class="wrap">
    <status-bar></status-bar>
    <image src="../../static/phoneBg.png" mode="widthFix" class="loginBg"></image>
    <image src="../../static/left.png" class="left" @click="goBack"></image>
    <view class="center">
      <view class="head">
        <view class="small">
          <image src="../../static/logo.png" class="img"></image>
        </view>
        <view class="title">欢迎登录投研宝</view>
      </view>
      <view class="box">
        <view class="input">
          <view class="area">+86</view>
          <view class="test">
            <uv-input type="number" v-model="phone" placeholder="请输入手机号" :maxlength='11' border="none" autoBlur fontSize='26rpx' color='#000000' placeholderStyle='font-size: 26rpx; color: #D5D5D5' @input="callInput"></uv-input>
          </view>
        </view>
        <view class="desc">首次登录将自动为您创建账号</view>
        <view class="protocol">
          <protocol-box :agree='agree' :protocol="protocol" @click="agree = !agree"></protocol-box>
        </view>
        <button class="btn phone-login" :disabled="!disabled" :loading="loading" hover-class='none' @click="next">{{ btnValue }}</button>
        <view class="continue" @click="goBack">继续一键登录</view>
        <login-footer :agree='agree'></login-footer>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { mobile, toast } from '@/common';
import { useOrder } from '@/store';

const iCloud = useOrder().ICloud;

const protocol = [{name: '《投研宝用户协议》', id: '5'}, {name: '《隐私政策》', id: '6'}]
let agree = ref(false);
let phone = ref('');
let disabled = ref(false);
let btnValue = ref('获取短信验证码');
let countdown = 60;
let timer = null;
let loading = ref(false);

function callInput(value){
  disabled.value = value.length == 11 && countdown == 60;
}
let isClick = false;
async function next(){
  const reg = /^(100\d{8})$/;
  if (!reg.test(phone.value) && !mobile(phone.value)) return toast('请输入正确的手机号');
  if(!agree.value) return toast('请勾选用户协议');
  if(isClick) return;
  isClick = true;
  disabled.value = false;
  loading.value = true;
  // 生成随机的验证码
  const code = Math.floor(Math.random() * 9000 + 1000).toString();
  
  const dataObj = {
    phone: phone.value,
    code: code,
  }
  iCloud.callFunction({
    name: 'touyanbaoSms',
    data: dataObj
  }).then(res => {
    if(res.result.code == 0){
      console.log(res.result);
      timer = setInterval(() => {
        if (countdown > 0) {
          countdown--;
          disabled.value = false;
          btnValue.value = `${countdown} 秒重新获取`;
        } else {
          isClick = false;
          countdown = 60;
          clearInterval(timer);
          disabled.value = true;
          btnValue.value = '重新获取验证码';
        }
      }, 1000);
      loading.value = false;
      uni.navigateTo({
        url: `/pages/login/code?phone=${phone.value}&code=${code}&time=${new Date().getTime()}`
      });
    }else{
      console.log(res.result);
      toast(res.result.msg)
    }
  });
}
function goBack(){
  uni.navigateBack({
    delta: 1
  });
}
</script>

<style lang="scss" scoped>
  .center{
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
    .box{
      background-color: #fff;
      border-top-left-radius: 50rpx;
      border-top-right-radius: 50rpx;
      padding: 0 50rpx;
      padding-top: 100rpx;
      position: relative;
      height: 75vh;
      .continue{
        font-weight: 400;
        font-size: 28rpx;
        color: #EF4734;
        margin-top: 30rpx;
        text-align: center;
      }
      .phone-login{
        width: 360rpx;
        margin: 0 auto;
      }
      .protocol{
        padding: 80rpx 0;
      }
      .desc{
        font-weight: 400;
        font-size: 24rpx;
        color: #898989;
        padding-top: 20rpx;
      }
      .input{
        border-bottom: 0.5px solid rgba(151, 151, 151, .3);
        width: 100%;
        padding-bottom: 20rpx;
        display: flex;
        align-items: center;
        .test{
          flex: 1;
        }
        .area{
          font-size: 34rpx;
          color: #000000;
        }
      }
    }
    .head{
      padding: 0 50rpx;
      padding-bottom: 30rpx;
      display: flex;
      align-items: center;
      .title{
        padding-left: 35rpx;
        font-weight: 500;
        font-size: 32rpx;
        color: #000000;
      }
      .small{
        width: 50rpx;
        height: 50rpx;
      }
    }
  }
  .left{
    width: 18rpx;
    height: 32rpx;
    margin-top: 30rpx;
    margin-left: 40rpx;
    position: relative;
    z-index: 2;
  }
  .loginBg{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 0;
  }
</style>